package com.zaizai.controller;
import com.zaizai.domain.AjaxResult;
import com.zaizai.utils.EmailApi;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.util.UUID;

/**
 * 邮箱Controller
 */
@Slf4j
@RestController
@RequestMapping("/email")
public class EmailController {
    @Resource
    private EmailApi emailApi;
    @Autowired
    private StringRedisTemplate redisTemplate;

    private String getResetPasswordTemplate(String captcha) {
        return String.format("""
            <div style="background-color: #f5f7fa; padding: 40px 0;">
                <div style="max-width: 600px; margin: 0 auto; background-color: #fff; border-radius: 8px; box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1); overflow: hidden;">
                    <div style="padding: 20px 48px; background-color: #fff; text-align: left; border-bottom: 1px solid #ebeef5;">
                        <img src="cid:logo" alt="Logo" style="height: 40px; width: auto;">
                    </div>
                    <div style="padding: 32px 48px; color: #333;">
                        <h2 style="margin: 0 0 24px; color: #303133; font-size: 20px;">重置密码验证</h2>
                        <p style="margin: 0 0 16px; color: #606266; font-size: 14px; line-height: 24px;">
                            您好！崽崽商城已经收到了您的密码重置请求。请使用以下验证码完成密码重置：
                        </p>
                        <div style="background-color: #f5f7fa; border-radius: 4px; padding: 16px; margin: 24px 0; text-align: center;">
                            <span style="font-size: 24px; font-weight: bold; color: #409eff; letter-spacing: 4px;">%s</span>
                        </div>
                        <p style="margin: 16px 0; color: #909399; font-size: 12px;">
                            验证码将在5分钟后失效，请尽快完成验证。如果这不是您的操作，请忽略此邮件。
                        </p>
                    </div>
                    <div style="padding: 16px 48px 32px; border-top: 1px solid #ebeef5;">
                        <p style="margin: 0; color: #909399; font-size: 12px; text-align: center;">
                            本邮件由系统自动发送，请勿直接回复。
                        </p>
                    </div>
                </div>
            </div>
            """, captcha);
    }

    private String getLoginVerificationTemplate(String captcha) {
        return String.format("""
            <div style="background-color: #f5f7fa; padding: 40px 0;">
                <div style="max-width: 600px; margin: 0 auto; background-color: #fff; border-radius: 8px; box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1); overflow: hidden;">
                    <div style="padding: 20px 48px; background-color: #fff; text-align: left; border-bottom: 1px solid #ebeef5;">
                        <img src="cid:logo" alt="Logo" style="height: 40px; width: auto;">
                    </div>
                    <div style="padding: 32px 48px; color: #333;">
                        <h2 style="margin: 0 0 24px; color: #303133; font-size: 20px;">登录验证</h2>
                        <p style="margin: 0 0 16px; color: #606266; font-size: 14px; line-height: 24px;">
                            欢迎使用崽崽商城！请使用以下验证码完成登录：
                        </p>
                        <div style="background-color: #f5f7fa; border-radius: 4px; padding: 16px; margin: 24px 0; text-align: center;">
                            <span style="font-size: 24px; font-weight: bold; color: #409eff; letter-spacing: 4px;">%s</span>
                        </div>
                        <p style="margin: 16px 0; color: #909399; font-size: 12px;">
                            验证码将在5分钟后失效，请尽快完成验证。为了保障您的账户安全，请勿将验证码泄露给他人。
                        </p>
                    </div>
                    <div style="padding: 16px 48px 32px; border-top: 1px solid #ebeef5;">
                        <p style="margin: 0; color: #909399; font-size: 12px; text-align: center;">
                            本邮件由系统自动发送，请勿直接回复。如果您没有进行相关操作，请忽略此邮件。
                        </p>
                    </div>
                </div>
            </div>
            """, captcha);
    }

    /**
     * 发送重置密码邮件
     *
     */
    @PostMapping("/resetPassword")
    public AjaxResult resetPassword(@RequestParam("email") String email) {
        String captchaString = UUID.randomUUID().toString().substring(0, 6);
        redisTemplate.opsForValue().set("resetPasswordCaptcha:" + email, captchaString, Duration.ofMinutes(5));
        log.info("发给{}的重置密码的验证码为：{}", email, captchaString);
        String emailContent = getResetPasswordTemplate(captchaString);
        if (emailApi.sendGeneralEmail("重置密码验证码", emailContent, email)) {
            return AjaxResult.success(captchaString);
        }
        return AjaxResult.error("邮箱发送失败");
    }

    /**
     * 发送登录或注册邮件
     */
    @PostMapping("/into")
    public AjaxResult into(@RequestParam("email") String email) {
        String captchaString = UUID.randomUUID().toString().substring(0, 6);
        redisTemplate.opsForValue().set("captcha:" + email, captchaString, Duration.ofMinutes(5));
        log.info("发给{}的验证码为：{}", email, captchaString);
        String emailContent = getLoginVerificationTemplate(captchaString);
        if (emailApi.sendGeneralEmail("登录验证码", emailContent, email)) {
            return AjaxResult.success(captchaString);
        }
        return AjaxResult.error("邮箱发送失败");
    }
}
